package com.wangsd.web.weixin;

import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.wangsd.common.component.WeixinPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "coupon", tags = "开通免充值优惠券")
@RestController
@RequestMapping("/wx/coupon")
@Slf4j
public class CouponController {

    @Autowired
    WeixinPayService weixinPayService;


    @ApiOperation(value = "创建订单", notes = "订单金额551和552", httpMethod = "POST")
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public String createOrder(@ApiParam(value = "金额", required = true) @RequestParam int amount) {
        WxPayService wxPayService = weixinPayService.getServiceExt(null, null);
        WxPayConfig payConfig = wxPayService.getConfig();
        //payConfig.setSubMchId("1554401591");
        wxPayService.setConfig(payConfig);

        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setBody("免充值优惠券001");
        String outTrandeNo = System.currentTimeMillis() + "";
        System.out.println("外部订单号=" + outTrandeNo);
        request.setOutTradeNo(outTrandeNo);
        request.setTotalFee(amount);
        request.setSpbillCreateIp("123.12.12.123");
        request.setNotifyUrl("http://www.example.com/wxpay/notify");
        request.setTradeType("NATIVE");
        //支付方式为NATIVE 需指定非空的product_id值
        request.setProductId(outTrandeNo);

        try {
            wxPayService.createOrder(request);
        } catch (WxPayException e) {
            return e.getMessage();
        }
        return outTrandeNo;
    }

    @ApiOperation(value = "查询订单", notes = "查询订单", httpMethod = "GET")
    @RequestMapping(value = "/queryOrder", method = RequestMethod.GET)
    public String queryOrder(@ApiParam(value = "外部订单编号", required = true) @RequestParam String outNO) {
        WxPayService wxPayService = weixinPayService.getServiceExt(null, null);
        WxPayConfig payConfig = wxPayService.getConfig();
        //payConfig.setSubMchId("1554401591");
        wxPayService.setConfig(payConfig);

        try {
            wxPayService.queryOrder("", outNO);
        } catch (WxPayException e) {
            return e.getMessage();
        }
        return "成功";
    }

    @ApiOperation(value = "退款申请", notes = "退款申请", httpMethod = "GET")
    @RequestMapping(value = "/refund", method = RequestMethod.GET)
    public String refund(@ApiParam(value = "外部订单编号", required = true) @RequestParam String outNO) {
        WxPayService wxPayService = weixinPayService.getServiceExt(null, null);
        WxPayConfig payConfig = wxPayService.getConfig();
        //payConfig.setSubMchId("1554401591");
        wxPayService.setConfig(payConfig);

        WxPayRefundRequest request2 = new WxPayRefundRequest();
        request2.setOutTradeNo(outNO);
        String outRefundNo = System.currentTimeMillis() + "";
        System.out.println("外部退款单号=" + outRefundNo);
        request2.setOutRefundNo(outRefundNo);
        request2.setTotalFee(552);
        request2.setRefundFee(551);
        try {
            wxPayService.refund(request2);
        } catch (WxPayException e) {
            return e.getMessage();
        }
        return "成功";
    }


    @ApiOperation(value = "查询退款", notes = "查询退款", httpMethod = "GET")
    @RequestMapping(value = "/refundQuery", method = RequestMethod.GET)
    public String refundQuery(@ApiParam(value = "外部订单编号", required = true) @RequestParam String outNO) {
        WxPayService wxPayService = weixinPayService.getServiceExt(null, null);
        WxPayConfig payConfig = wxPayService.getConfig();
        //payConfig.setSubMchId("1554401591");
        wxPayService.setConfig(payConfig);

        try {
            wxPayService.refundQuery("", outNO, "", "");
        } catch (WxPayException e) {
            return e.getMessage();
        }
        return "成功";
    }

    @ApiOperation(value = "下载对账单", notes = "下载对账单", httpMethod = "GET")
    @RequestMapping(value = "/downloadBill", method = RequestMethod.GET)
    public String downloadBill(@ApiParam(value = "日期20190901", required = true) @RequestParam String date) {
        WxPayService wxPayService = weixinPayService.getServiceExt(null, null);
        WxPayConfig payConfig = wxPayService.getConfig();
        //payConfig.setSubMchId("1554401591");
        wxPayService.setConfig(payConfig);

        try {
            wxPayService.downloadBill(date, "ALL", "", "");
        } catch (WxPayException e) {
            return e.getMessage();
        }
        return "成功";
    }


}
